package de.szut.sudoku.gui.sudokufield;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Abstract class for each key listener
 * 
 * @author Marc Huisinga
 * 
 */
public abstract class SudokuKeyListener extends KeyAdapter {
	private int[] allowedNumberKeys;
	private KeyEvent keyEvent;

	public KeyEvent getKeyEvent() {
		return keyEvent;
	}

	public void setKeyEvent(KeyEvent keyEvent) {
		this.keyEvent = keyEvent;
	}

	/**
	 * Notes what keys are allowed for number input
	 */
	public SudokuKeyListener() {
		int[] allowedKeys = { KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3,
				KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7,
				KeyEvent.VK_8, KeyEvent.VK_9, KeyEvent.VK_NUMPAD1,
				KeyEvent.VK_NUMPAD2, KeyEvent.VK_NUMPAD3, KeyEvent.VK_NUMPAD4,
				KeyEvent.VK_NUMPAD5, KeyEvent.VK_NUMPAD6, KeyEvent.VK_NUMPAD7,
				KeyEvent.VK_NUMPAD8, KeyEvent.VK_NUMPAD9 };
		this.allowedNumberKeys = allowedKeys;
	}

	/**
	 * Checks if the input key is a number key
	 * 
	 * @return
	 */
	public boolean isNumber() {
		for (int currentKey : allowedNumberKeys) {
			if (currentKey == keyEvent.getKeyCode()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Checks if the user input a reset button
	 * 
	 * @return
	 */
	public boolean isReset() {
		if (keyEvent.getKeyCode() == KeyEvent.VK_BACK_SPACE
				|| keyEvent.getKeyCode() == KeyEvent.VK_SPACE
				|| keyEvent.getKeyCode() == KeyEvent.VK_0) {
			return true;
		}
		return false;
	}

	/**
	 * Simulates the click event
	 */
	@Override
	public abstract void keyPressed(KeyEvent keyEvent);
}
